﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WFNetLib;
using System.Collections;
using System.Threading;
using System.Net;
using System.Runtime.Serialization.Json;
using System.IO;
using WFNetLib.Log;

namespace TempDataInterfaceDLL
{
    public partial class TempDataInterface
    {
        string SoftVer = "20160103";
        public delegate void ShowTextCallbackDelegate(string str);
        public ShowTextCallbackDelegate[] ShowText = new ShowTextCallbackDelegate[CombinationThreadCount];


        public delegate void NewTempDataCallbackDelegate(TempData td);
        public NewTempDataCallbackDelegate[] NewTempData = new NewTempDataCallbackDelegate[CombinationThreadCount];

        public delegate void NewCallDataCallbackDelegate(TempData td);
        public NewCallDataCallbackDelegate[] NewCallData = new NewCallDataCallbackDelegate[CombinationThreadCount];

        private string[] UserName;
        private string[] Password;
        WFHttpWebResponse[] WebResponse;
        private DateTime[] LastGetTime;

        private Hashtable IntCalls;
        private Hashtable DecimalCalls;
        private Hashtable NoticeCalls;
        private bool bPeriodGetDataThreadStop;
        private bool bPeriodGetDataThreadIsStop;

        public Thread PeriodGetDataThreadIns = null;
        private int count;
        const int maxGetdataThread = 3;
        //int[] pageCount = new int[30];
        int[] pageIndex = new int[30];
        List<CallData>[] GetDatas = new List<CallData>[30];

        const int CombinationThreadCount= 10;
        OldTempData[] saveOldTempData = new OldTempData[CombinationThreadCount];
        OldTempData[] curOldTempData = new OldTempData[CombinationThreadCount];
        bool[] CombinationThreadWorked=new bool[CombinationThreadCount];
        bool[] CombinationThreadIsWork = new bool[CombinationThreadCount];
        bool[] CombinationFirst = new bool[CombinationThreadCount];
        Thread[] ConbinationThreadIns=new Thread[CombinationThreadCount];
        CombinationData[] CombinationDataList = new CombinationData[CombinationThreadCount];
        public void StartPeriodGetDataProc()
        {
            if (!bPeriodGetDataThreadIsStop)
                return;
            try
            {
                OldTempData olt = OldTempDataDBOption.GetLastOldTemp();
                for (int i = 0; i < 30; i++)
                {
                    LastGetTime[i] = olt.CallInTime.AddSeconds(1);
                }
            }
            catch (Exception ex)
            {
                ShowTextLogProc("服务启动失败:"+ex.Message, 0);
                throw ex;
            }
            GetDataThreadIns = new Thread[maxGetdataThread];
            isGetData = new bool[maxGetdataThread];
            for (int i = 0; i < maxGetdataThread; i++)
                isGetData[i] = false;
            bPeriodGetDataThreadStop = false;
            bPeriodGetDataThreadIsStop = false;
            count = 0;
            PeriodGetDataThreadIns = new Thread(new ThreadStart(PeriodGetDataProc));
            PeriodGetDataThreadIns.Name = "周期访问端口";
            ShowTextLogProc("周期访问端口开始---"+SoftVer,0);
            PeriodGetDataThreadIns.Start();
        }
        public void StopPeriodGetDataProc()
        {
            while (true)
            {
                for (int i = 0; i < maxGetdataThread; i++)
                {
                    if (isGetData[i])
                    {
                        GetDataThreadIns[i].Abort();
                    }
                }
                break;
            }
            bPeriodGetDataThreadStop = true;
            bPeriodGetDataThreadIsStop = true;
            PeriodGetDataThreadIns.Abort();
            //while (!bPeriodGetDataThreadIsStop) ;
        }
        
        public void CheckData()
        {            
            try
            {
                //获取呼叫号
                List<CallData> Notices = GetDatas[29];
                if (Notices != null)
                {
                    //for (int j = (calls.Count-1); j >=0; j--)
                    for (int j = 0; j < Notices.Count; j++)
                    {
                        List<CallData> noticeCalls = (List<CallData>)NoticeCalls[Notices[j].caller];
                        if (noticeCalls != null)
                        {
                            noticeCalls.Add(Notices[j]);
                        }
                        else
                        {
                            noticeCalls = new List<CallData>();
                            noticeCalls.Add(Notices[j]);
                        }
                        ShowTextLogProc1(Notices[j].caller, "呼叫:" + Notices[j].callin_time);
                        NoticeCalls[Notices[j].caller] = noticeCalls;
                        StringBuilder sb = new StringBuilder();
                        sb.Append("---------------获得呼叫号-------------------");
                        sb.Append("\r\n");
                        sb.Append(Notices[j].caller);
                        sb.Append("\r\n");
                        sb.Append(Notices[j].callin_time.ToString());
                        sb.Append("\r\n");
                        sb.Append("----------------------------------------------");
                        ShowTextLogProc(sb.ToString(), 0);
                        ShowTextLogProc1(Notices[j].caller, sb.ToString());
                    }
                }
                //获得整数温度
                for (int i = 0; i < 19; i++)
                {
                    List<CallData> calls = GetDatas[i];
                    if (calls != null)
                    {
                        //for (int j = (calls.Count-1); j >=0; j--)
                        for (int j = 0; j < calls.Count; j++)
                        {
                            List<CallData> intCalls = (List<CallData>)IntCalls[calls[j].caller];
                            if (intCalls != null)
                            {
                                intCalls.Add(calls[j]);
                            }
                            else
                            {
                                intCalls = new List<CallData>();
                                intCalls.Add(calls[j]);
                            }
                            ShowTextLogProc1(calls[j].caller, "用户" + i.ToString() + ":" + calls[j].callin_time);
                            IntCalls[calls[j].caller] = intCalls;
                            //                         ShowTextLogProc("---------------获得整数-------------------");
                            //                         ShowTextLogProc(calls[j].caller);
                            //                         ShowTextLogProc((calls[j].UserIndex + 11).ToString());
                            //                         ShowTextLogProc(calls[j].callin_time.ToString());
                            //                         ShowTextLogProc("----------------------------------------------");
                        }
                    }
                }
                //获得小数温度
                for (int i = 19; i < 29; i++)
                {
                    List<CallData> calls = GetDatas[i];
                    if (calls != null)
                    {
                        //for (int j = (calls.Count-1); j >=0; j--)
                        for (int j = 0; j < calls.Count; j++)
                        {
                            List<CallData> decimalCalls = (List<CallData>)DecimalCalls[calls[j].caller];
                            if (decimalCalls != null)
                            {
                                decimalCalls.Add(calls[j]);
                            }
                            else
                            {
                                decimalCalls = new List<CallData>();
                                decimalCalls.Add(calls[j]);
                            }
                            ShowTextLogProc1(calls[j].caller, "用户" + i.ToString() + ":" + calls[j].callin_time);
                            DecimalCalls[calls[j].caller] = decimalCalls;
                            //                         ShowTextLogProc("---------------获得小数-------------------");
                            //                         ShowTextLogProc(calls[j].caller);
                            //                         ShowTextLogProc((calls[j].UserIndex - 19).ToString());
                            //                         ShowTextLogProc(calls[j].callin_time.ToString());
                            //                         ShowTextLogProc("----------------------------------------------");
                        }
                    }
                }
                Combination();
                ShowTextLogProc("---------------完成一次扫描--------", 0);
            }
            catch (System.Exception ex)
            {
                ShowTextLogProc("CheckData发生重要错误" + ex.Message, 0);
            }
        }        
        int GetEarlyDecamal(List<CallData> decimalCalls)
        {
            DateTime dt = decimalCalls[0].callin_time;
            int ret=0;
            for (int i = 1; i < decimalCalls.Count; i++)
            {
                if (dt > decimalCalls[i].callin_time)
                {
                    ret = i;
                    dt = decimalCalls[i].callin_time;
                }
            }
            return ret;
        }
        DateTime GetMinLastGetTime()
        {
            DateTime dt = LastGetTime[19];
            for (int i = 20; i < 29; i++)
            {
                if (dt > LastGetTime[i])
                    dt = LastGetTime[i];
            }
            return dt;
        }
        bool Login(int index)
        {
            try
            {
                HttpWebResponse hr = WebResponse[index].CreateGetHttpResponse("http://www.c4006.com/user/servlet/verify.tslogin");
                //                 if(ShowText!=null)
                //                     ShowText(UserName[index] + WebResponse[index].Content);
                // {\"message\":\"8415\",\"data\":{},\"success\":true}
                DataContractJsonSerializer s = new DataContractJsonSerializer(typeof(verify));
                MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(WebResponse[index].Content));
                verify v = (verify)s.ReadObject(stream);
                string url = "http://www.c4006.com/user/servlet/login.tslogin?userName=admin@";
                url += UserName[index];
                url += "&passWord=";
                url += Password[index];
                url += "&verifyCode=";
                url += v.message;
                url += "&verifyHidden=";
                url += v.message;
                url += "";
                hr = WebResponse[index].CreateGetHttpResponse(url);
                //                 if (ShowText != null)
                //                     ShowText(UserName[index] + WebResponse[index].Content);
                //{\"message\":\"index\",\"data\":{},\"success\":true} 
                stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(WebResponse[index].Content));
                s = new DataContractJsonSerializer(typeof(login));
                login l = (login)s.ReadObject(stream);
                url = "http://www.c4006.com/" + l.message;
                hr = WebResponse[index].CreateGetHttpResponse(url);
                //ShowText(UserName[index] + WebResponse[index].Content);
                ShowTextLogProc("用户"+index.ToString() + "成功登陆!",index%CombinationThreadCount);
            }
            catch (System.Exception ex)
            {
                //ErrLog(ex.Message);
                //ShowText(ex.Message + "....");
                ShowTextLogProc("用户" + index.ToString() + "登陆失败--->" + ex.Message, index%CombinationThreadCount);
                return false;
            }
            return true;
        }        
        public TempDataInterface(DateTime LastDateTime)
        {
            bPeriodGetDataThreadIsStop = true;
            bPeriodGetDataThreadStop = false;
            UserName = new string[30];
            Password = new string[30];

            //11度到19度温度点
            UserName[0] = "4000011254";//11
            Password[0] = "2Wv4Bf";
            UserName[1] = "4000011274";//12
            Password[1] = "AT2DQi";
            UserName[2] = "4000011324";//13
            Password[2] = "DB4PPG";
            UserName[3] = "4000011364";//14
            Password[3] = "kqQW7f";
            UserName[4] = "4000011374";//15
            Password[4] = "FdcdQ4";
            UserName[5] = "4000011424";//16
            Password[5] = "ATLHGh";
            UserName[6] = "4000011434";//17
            Password[6] = "IkLNUQ";
            UserName[7] = "4000011474";//18
            Password[7] = "fsCEVg";
            UserName[8] = "4000011524";//19
            Password[8] = "DfnYUT";

            //20度到29度温度点
            UserName[9] = "4000011534";//20
            Password[9] = "4RFDZK";
            UserName[10] = "4000011564";//21
            Password[10] = "TDlwFh";
            UserName[11] = "4000011574";//22
            Password[11] = "CaEpnX";
            UserName[12] = "4000011594";//23
            Password[12] = "9moYWy";
            UserName[13] = "4000011614";//24
            Password[13] = "j2qGb6";
            UserName[14] = "4000011624";//25
            Password[14] = "imyXPX";
            UserName[15] = "4000011634";//26
            Password[15] = "ZzeJDu";
            UserName[16] = "4000011704";//27
            Password[16] = "IRJlvR";
            UserName[17] = "4000011714";//28
            Password[17] = "9mP8b5";
            UserName[18] = "4000011724";//29
            Password[18] = "InvpDT"; 

            //小数温度点
            UserName[19] = "4000413804";//0
            Password[19] = "4QjGjY";
            UserName[20] = "4000413814";//1
            Password[20] = "EwuEbv";
            UserName[21] = "4000413824";//2
            Password[21] = "FH48O3";
            UserName[22] = "4000413834";//3
            Password[22] = "cfp3pw";
            UserName[23] = "4000414154";//4
            Password[23] = "ZRwJdE";
            UserName[24] = "4000413854";//5
            Password[24] = "grN9bu";
            UserName[25] = "4000413864";//6
            Password[25] = "GvXOlH";
            UserName[26] = "4000413874";//7
            Password[26] = "YCoweF";
            UserName[27] = "4000414124";//8
            Password[27] = "HBdDuS";
            UserName[28] = "4000414134";//9
            Password[28] = "oZEQbT";
            //申告号码
            UserName[29] = "4000011074";//
            Password[29] = "VTNZR4";

            for (int i = 0; i < CombinationThreadCount; i++)
            {
                ShowText[i] = null;
                NewCallData[i] = null;
                NewTempData[i] = null;
            }

            WebResponse = new WFHttpWebResponse[30];
            IntCalls = new Hashtable();
            //OldTemp = new Hashtable();
            DecimalCalls = new Hashtable();
            NoticeCalls = new Hashtable();
            LastGetTime = new DateTime[30];
            for (int i = 0; i < 30; i++)
            {
                LastGetTime[i] = LastDateTime.AddSeconds(1);
            }            
            for (int i = 0; i < 30; i++)
            {
                WebResponse[i] = new WFHttpWebResponse();             
            }
            WFNetLib.ADO.AccessOP.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\温度组合用历史温度.accdb;Persist Security Info=False";
        }
        private void ShowTextLogProc(string str,int index)
        {
            str = DateTime.Now.ToString("HH:mm:ss:ffff") + ":  " + str;
            TextLog.AddTextLog(str);
            if (ShowText[index] != null)
                ShowText[index](str);
        }
        private void ShowTextLogProc1(string call,string str)
        {
            str = DateTime.Now.ToString("HH:mm:ss:ffff") + ":  " + str;
            TextLog.AddTextLog(str, "d:\\组合温度服务日志\\按电话号\\"+call+".txt",false);
        }
        public static ReBiaoData GetReBiaoData()
        {
            ReBiaoData rbd = new ReBiaoData();
            return rbd;
        }
    }
    public class CombinationData
    {
        public CallData[] decimalCalls;
        public CallData[] intCalls;
        public CallData[] noticeCalls;
        public string caller;
        public int index;
    }
}
